package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.PlayerInterface;
import model.SettingsInterface;
import model.UserInterface;
import toolkit.Debug;

// TODO: Auto-generated Javadoc
/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class Options extends View {
	
	/** Tabs pane */
	private JTabbedPane tabspanel;
	
	/** Panels */
	private JPanel panelSettings;
	private JPanel panelAccount;
	
	/** Labels for heads */
	private JLabel labelDisplay;
	private JLabel labelSound;
	
	/** Labels for password fields */
	private JLabel labelTypePassword;
	private JLabel labelConfirmPassword;
	private JLabel labelChangePassword;
	
	/** Password fields */
	private JPasswordField fieldPassword;
	private JPasswordField fieldConfirmPassword;
	
	/** The checkbox animation. */
	private JCheckBox checkboxAnimation;
	private JCheckBox checkboxAutoMuck;
	private JCheckBox checkboxSound;	
	
	/** Buttons */
	private JButton buttonSettingsSave;
	private JButton buttonSettingsCancel;
	private JButton buttonAccountSave;
	private JButton buttonAccountCancel;

	protected controller.Options controller;
	
	private boolean isPlayer;
	
	/**
	 * Auto-generated main method to display this
	 * JPanel inside a new JFrame.
	 * 
	 * @param args the args
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new Options());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	/**
	 * Instantiates a new options.
	 */
	public Options() {
		super();
		initGUI();
	}
	
	public Options(controller.Options controller) {
		super();
		
		this.controller = controller;
		
		UserInterface user = controller.getUser();
		
		isPlayer = user instanceof PlayerInterface;
		
		initGUI();
		
		if (isPlayer)
		{
			SettingsInterface settings = ((PlayerInterface) user).getSettings();
			
			checkboxAnimation.setSelected(settings.useAnimations());
			checkboxAutoMuck.setSelected(settings.useAutoMuck());
			checkboxSound.setSelected(settings.useSound());
		}
	}
	
	/**
	 * Inits the gui.
	 */
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(313, 256));
			this.setLayout(null);
			this.setSize(313, 256);
			{
				tabspanel = new JTabbedPane();
				this.add(tabspanel);
				tabspanel.setBounds(12, 12, 289, 232);
				
				if (isPlayer)
				{
					panelSettings = new JPanel();
					tabspanel.addTab("Settings", null, panelSettings, "Audiovisual settings");
					panelSettings.setBounds(248, 55, 234, 189);
					panelSettings.setSize(224, 256);
					panelSettings.setLayout(null);
					panelSettings.setPreferredSize(new java.awt.Dimension(224, 256));
					{
						buttonSettingsCancel = new JButton();
						panelSettings.add(buttonSettingsCancel);
						buttonSettingsCancel.setBounds(216, 173, 56, 21);
						buttonSettingsCancel.setLayout(null);
						buttonSettingsCancel.setText("Cancel");
						buttonSettingsCancel.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								buttonSettingsCancelActionPerformed(evt);
							}
						});
					}
					{
						buttonSettingsSave = new JButton();
						panelSettings.add(buttonSettingsSave);
						buttonSettingsSave.setBounds(154, 173, 56, 21);
						buttonSettingsSave.setLayout(null);
						buttonSettingsSave.setText("Save");
						buttonSettingsSave.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								buttonSettingsSaveActionPerformed(evt);
							}
						});
					}
					{
						labelDisplay = new JLabel();
						panelSettings.add(labelDisplay);
						labelDisplay.setBounds(12, 12, 260, 14);
						labelDisplay.setLayout(null);
						labelDisplay.setText("Display");
						labelDisplay.setFont(new java.awt.Font("Tahoma",1,11));
					}
					{
						checkboxAnimation = new JCheckBox();
						panelSettings.add(checkboxAnimation);
						checkboxAnimation.setBounds(12, 32, 124, 18);
						checkboxAnimation.setLayout(null);
						checkboxAnimation.setText("Enable animation");
						checkboxAnimation.setToolTipText("Enable card animations.");
					}
					{
						checkboxAutoMuck = new JCheckBox();
						panelSettings.add(checkboxAutoMuck);
						checkboxAutoMuck.setBounds(12, 50, 124, 18);
						checkboxAutoMuck.setLayout(null);
						checkboxAutoMuck.setText("Auto-muck");
						checkboxAutoMuck.setToolTipText("Either show your hand or not at the end of the game.");
					}
					{
						labelSound = new JLabel();
						panelSettings.add(labelSound);
						labelSound.setBounds(12, 74, 260, 14);
						labelSound.setLayout(null);
						labelSound.setText("Sound");
						labelSound.setFont(new java.awt.Font("Tahoma",1,11));
					}
					{
						checkboxSound = new JCheckBox();
						panelSettings.add(checkboxSound);
						checkboxSound.setBounds(12, 94, 124, 18);
						checkboxSound.setLayout(null);
						checkboxSound.setText("Enable sounds");
						checkboxSound.setToolTipText("Enable sounds.");

					}
				}
				
				{
					panelAccount = new JPanel();
					tabspanel.addTab("Account", null, panelAccount, "User account settings");
					panelAccount.setBounds(12, 55, 331, 189);
					panelAccount.setSize(224, 192);
					panelAccount.setLayout(null);
					{
						buttonAccountCancel = new JButton();
						panelAccount.add(buttonAccountCancel);
						buttonAccountCancel.setBounds(216, 173, 56, 21);
						buttonAccountCancel.setText("Cancel");
						buttonAccountCancel.setToolTipText("Cancels the changes.");
						buttonAccountCancel.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								buttonAccountCancelActionPerformed(evt);
							}
						});
					}
					{
						buttonAccountSave = new JButton();
						panelAccount.add(buttonAccountSave);
						buttonAccountSave.setBounds(154, 173, 56, 21);
						buttonAccountSave.setText("Save");
						buttonAccountSave.setToolTipText("Saves the changes.");
						buttonAccountSave.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent evt) {
								buttonAccountSaveActionPerformed(evt);
							}
						});
					}
					{
						labelChangePassword = new JLabel();
						panelAccount.add(labelChangePassword);
						labelChangePassword.setBounds(12, 12, 260, 14);
						labelChangePassword.setText("Change password");
						labelChangePassword.setFont(new java.awt.Font("Tahoma",1,11));
					}
					{
						labelTypePassword = new JLabel();
						panelAccount.add(labelTypePassword);
						labelTypePassword.setBounds(12, 38, 111, 14);
						labelTypePassword.setText("New password");
					}
					{
						labelConfirmPassword = new JLabel();
						panelAccount.add(labelConfirmPassword);
						labelConfirmPassword.setBounds(12, 65, 111, 14);
						labelConfirmPassword.setText("Confirm password");
					}
					{
						fieldPassword = new JPasswordField();
						panelAccount.add(fieldPassword);
						fieldPassword.setBounds(129, 34, 143, 22);
						fieldPassword.setText("");
						fieldPassword.setToolTipText("Type your desired password.");
					}
					{
						fieldConfirmPassword = new JPasswordField();
						panelAccount.add(fieldConfirmPassword);
						fieldConfirmPassword.setBounds(129, 61, 143, 22);
						fieldConfirmPassword.setText("");
						fieldConfirmPassword.setToolTipText("Type the password again, to avoid mistakes.");
					}
				}
			}
			
			ChangeListener tabsChangeListener = new ChangeListener() {
				public void stateChanged(ChangeEvent changeEvent) {
					if (tabspanel.getSelectedComponent().equals(panelAccount)) {
						setDefaultButton(buttonAccountSave);
					}
					else if (tabspanel.getSelectedComponent().equals(panelSettings)) {
						setDefaultButton(buttonSettingsSave);
					}
				}
			};
		    tabspanel.addChangeListener(tabsChangeListener);
		    
		    // Hack: because we cannot fire state changed before the tabbed pane has been set visible
		    // we invoke it manually.
			tabsChangeListener.stateChanged(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void buttonSettingsSaveActionPerformed(ActionEvent evt) {
		controller.saveSettings(checkboxAnimation.isSelected(), checkboxAutoMuck.isSelected(), checkboxSound.isSelected());
	}
	
	private void buttonSettingsCancelActionPerformed(ActionEvent evt) {
		controller.cancel();
	}
	
	private void buttonAccountCancelActionPerformed(ActionEvent evt) {
		controller.cancel();
	}
	
	private void buttonAccountSaveActionPerformed(ActionEvent evt) {
		controller.saveAccountSettings(fieldPassword.getPassword(), fieldConfirmPassword.getPassword());
	}

}
